package com.nanyang.tms.manager.bill.handler;

import cn.hutool.core.util.NumberUtil;
import com.nanyang.common.core.flow.context.TransHandlerContext;
import com.nanyang.common.core.flow.handler.AbstractTransHandler;
import com.nanyang.common.tool.utils.uuid.GlobalIdGeneratorUtil;
import com.nanyang.tms.common.contstant.TmsSerializeConstant;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.waybill.WaybillIncomeFeeDao;
import com.nanyang.tms.dao.waybill.WaybillPayFeeDao;
import com.nanyang.tms.domain.bill.context.BillUpdateContext;
import com.nanyang.tms.domain.cost.context.CalculateCostFreightContext;
import com.nanyang.tms.domain.forecast.context.CalculateFreightContext;
import com.nanyang.tms.domain.waybill.WaybillIncomeFeeDO;
import com.nanyang.tms.domain.waybill.WaybillPayFeeDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class BillFeeHandler extends AbstractTransHandler{
	@Autowired
	private WaybillPayFeeDao waybillPayFeeDao;
	@Autowired
	private WaybillIncomeFeeDao waybillIncomeFeeDao;
	@Autowired
	private GlobalIdGeneratorUtil globalIdGeneratorUtil;
	@Override
	public boolean isAsync() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean handle(TransHandlerContext context) {
		//成本运费
		CalculateCostFreightContext calculateCostFreightContext = (CalculateCostFreightContext) context.getInnerContext(CalculateCostFreightContext.class);
		//销售运费
		CalculateFreightContext calculateFreightContext = (CalculateFreightContext) context.getInnerContext(CalculateFreightContext.class);
		//运单编码
		BillUpdateContext billUpdateContext = (BillUpdateContext)context.getInnerContext(BillUpdateContext.class);

		try {
			//支出费用
			payFee(calculateCostFreightContext,billUpdateContext);
			//收入费用
			incomeFee(calculateFreightContext,billUpdateContext);
			return true;
		} catch (Exception e) {
			throw TmsExceptionUtil.wrapException(e);
		}
		

	}

	private void payFee(CalculateCostFreightContext calculateCostFreightContext,BillUpdateContext billUpdateContext){
		Double aDouble = waybillPayFeeDao.sumPayFee(billUpdateContext.getWaybillCode());
		BigDecimal amount = calculateCostFreightContext.getAmount();
		BigDecimal sub = NumberUtil.sub(amount, aDouble);
		if(sub.doubleValue()!=0){
			WaybillPayFeeDO waybillPayFeeDO = new WaybillPayFeeDO();
			String payCode = TmsSerializeConstant.PAY_FEE_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.PAY_FEE_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
			waybillPayFeeDO.setPayCode(payCode);
			waybillPayFeeDO.setWaybillCode(billUpdateContext.getWaybillCode());
			waybillPayFeeDO.setCompanyCode(billUpdateContext.getCompanyCode());
			waybillPayFeeDO.setFeeName("运费");
			waybillPayFeeDO.setFeeCode("FREIGHT");
			waybillPayFeeDO.setPredictAmount(new BigDecimal("0"));
			waybillPayFeeDO.setPayable(sub);
			waybillPayFeeDO.setPayment(sub);
			waybillPayFeeDO.setIp(billUpdateContext.getIp());
			waybillPayFeeDO.setGmtCreatePin(billUpdateContext.getPin());
			waybillPayFeeDao.insert(waybillPayFeeDO);
		}

	}
	private void incomeFee(CalculateFreightContext calculateFreightContext,BillUpdateContext billUpdateContext){
		Double aDouble = waybillIncomeFeeDao.sumIncomeFee(billUpdateContext.getWaybillCode());
		BigDecimal amount = calculateFreightContext.getAmount();
		BigDecimal sub = NumberUtil.sub(amount,aDouble);
		if(sub.doubleValue()!=0){
			WaybillIncomeFeeDO waybillIncomeFeeDO = new WaybillIncomeFeeDO();
			String incomeCode = TmsSerializeConstant.INCOME_FEE_KEY+globalIdGeneratorUtil.getNextSeq(TmsSerializeConstant.INCOME_FEE_SEQ_KEY, TmsSerializeConstant.SEQ_INCRBY);
			waybillIncomeFeeDO.setIncomeCode(incomeCode);
			waybillIncomeFeeDO.setWaybillCode(billUpdateContext.getWaybillCode());
			waybillIncomeFeeDO.setCompanyCode(billUpdateContext.getCompanyCode());
			waybillIncomeFeeDO.setFeeName("运费");
			waybillIncomeFeeDO.setFeeCode("FREIGHT");
			waybillIncomeFeeDO.setPredictAmount(new BigDecimal("0"));
			waybillIncomeFeeDO.setReceivable(sub);
			waybillIncomeFeeDO.setReceipts(sub);
			waybillIncomeFeeDO.setIp(billUpdateContext.getIp());
			waybillIncomeFeeDO.setGmtCreatePin(billUpdateContext.getPin());
			waybillIncomeFeeDao.insert(waybillIncomeFeeDO);
			billUpdateContext.setAmount(sub);
		}

	}
}
